package cn.tedu.controller;

import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.Arrays;


@RestController
@RequestMapping("stu")
public class sController {
    @RequestMapping("add")
    public Object add(Student s){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/cgb2106?characterEncoding=utf8";
            Connection connection = DriverManager.getConnection(url, "root", "root");
            String sql= "insert into tb_student values(null,?,?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,s.getName());
            preparedStatement.setObject(2,s.getAge());
            preparedStatement.setObject(3,s.getGender());
            preparedStatement.setObject(4, Arrays.toString(s.getHobby()));
            preparedStatement.setObject(5,s.getDegree());
            preparedStatement.setObject(6,s.getDate());
            preparedStatement.executeUpdate();
            System.out.println("数据插入成功");
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

}
